package com.qiang.demo04;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Test06 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //解析字符串。转换成date类型
        String s = "2020-11-12";
        Date parse = sdf.parse(s);
        System.out.println(parse);

        System.out.println("===================");
        //获取年
        int year = parse.getYear();
        System.out.println(year);
        System.out.println("===================");

        //使用LocaDateTime转换
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        //解析字符串，转换成日期
        LocalDate parse1 = LocalDate.parse(s, dtf);
        System.out.println(parse1);
        System.out.println("===================");


        //给定时间
        LocalDate of = LocalDate.of(1993, 05, 20);

        //把字符串转换成日期
        String format = of.format(dtf);
        System.out.println(format);
        System.out.println("===================");


        //封装，
        Student stu = new Student("张三", LocalDate.parse("1993-05-05", dtf));
        int year1 = stu.getBirthday().getYear();
        System.out.println("出生年是：");
        System.out.println(year1);
        System.out.println("===================");
    }
}
